﻿using System.IO;

namespace Red.Launcher.Utilities
{
    public static class Files
    {
        public static void ClearAttributes( this FileSystemInfo fileSystemInfo )
        {
            var directoryInfo = fileSystemInfo as DirectoryInfo;
            if ( directoryInfo != null )
            {
                foreach ( FileSystemInfo childInfo in directoryInfo.GetFileSystemInfos() )
                {
                    childInfo.ClearAttributes();
                }
            }

            fileSystemInfo.Attributes = FileAttributes.Normal;
        }

        public static void Copy( DirectoryInfo source, DirectoryInfo target, bool recurse = true )
        {
            if ( recurse )
            {
                foreach ( DirectoryInfo dir in source.GetDirectories() )
                {
                    Copy( dir, target.CreateSubdirectory( dir.Name ) );
                }
            }

            foreach ( FileInfo file in source.GetFiles() )
            {
                file.CopyTo( Path.Combine( target.FullName, file.Name ), true );
            }
        }

        public static void RecursiveDelete( this DirectoryInfo directory )
        {
            DirectoryInfo[] childDirectories = directory.GetDirectories();

            foreach ( DirectoryInfo child in childDirectories )
            {
                RecursiveDelete( child );
            }

            FileInfo[] files = directory.GetFiles();

            foreach ( FileInfo file in files )
            {
                file.Attributes = FileAttributes.Normal;
                file.Delete();
            }

            directory.Attributes = FileAttributes.Normal;
            directory.Delete();
        }
    }
}
